/**  
 * GalleryAdapter.java
 * @version 1.0
 * @author Haven
 * @createTime 2011-12-9 下午05:04:34
 */
package com.creditease.rss.ui.adapter;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;

import com.creditease.rss.RssConst;
import com.creditease.rss.ui.MyImageView;
import com.creditease.rss.ui.R;
import com.creditease.rss.util.AsyncImageLoader3;
import com.creditease.rss.util.FileUtil;

public class GalleryAdapter extends BaseAdapter {

    private Context context;
    private String[] images;
    private AsyncImageLoader3 asyncImageLoader3 = new AsyncImageLoader3();

    public GalleryAdapter(Context context, String[] list) {
	this.context = context;
	this.images = list;
    }

    @Override
    public int getCount() {
	return images.length;
    }

    @Override
    public Object getItem(int position) {
	return images[position];
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

	String path = images[position];
	if (!path.startsWith("http")) {
	    Bitmap bmp = BitmapFactory.decodeFile(images[position]);
	    MyImageView view = new MyImageView(context, bmp.getWidth(),
		    bmp.getHeight());
	    view.setLayoutParams(new Gallery.LayoutParams(
		    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	    view.setImageBitmap(bmp);
	    return view;
	} else {
	    String newFileName = FileUtil.getFileName(path);
	    // 后缀
	    final String filePath = RssConst.PIC_TMP_DIR + newFileName;
	    File file = new File(filePath);
	    if (file.exists()) {
		Bitmap bmp = BitmapFactory.decodeFile(filePath);
		MyImageView view = new MyImageView(context, bmp.getWidth(),
			bmp.getHeight());
		view.setLayoutParams(new Gallery.LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		view.setImageBitmap(bmp);
		return view;
	    }
	    Drawable drawable = asyncImageLoader3.loadDrawable(path,
		    new AsyncImageLoader3.ImageCallback() {
			public void imageLoaded(Drawable imageDrawable) {
			    if (imageDrawable != null) {
				// 保存图片
				try {
				    BitmapDrawable bd = (BitmapDrawable) imageDrawable;
				    Bitmap bmp = bd.getBitmap();
				    FileOutputStream fos = new FileOutputStream(
					    filePath);
				    bmp.compress(Bitmap.CompressFormat.JPEG,
					    100, fos);
				    fos.flush();
				    fos.close();
				} catch (Exception e) {
				    e.printStackTrace();
				}
			    }
			}
		    });
	    BitmapDrawable bd = (BitmapDrawable) drawable;
	    if (bd != null) {
		Bitmap bmp = bd.getBitmap();
		MyImageView view = new MyImageView(context, bmp.getWidth(),
			bmp.getHeight());
		view.setLayoutParams(new Gallery.LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		view.setImageBitmap(bmp);
		return view;
	    } else {
		Bitmap bmp = BitmapFactory.decodeResource(
			context.getResources(), R.drawable.icon_home);
		MyImageView view = new MyImageView(context, bmp.getWidth(),
			bmp.getHeight());
		view.setLayoutParams(new Gallery.LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		view.setImageBitmap(bmp);
		return view;
	    }
	}
    }
}
